package com.stox.shared.indicator.model;

import com.stox.shared.indicator.model.config.BaseIndicatorConfig;
import com.stox.shared.indicator.model.enumeration.IndicatorDisplayType;
import com.stox.shared.indicator.model.enumeration.IndicatorPosition;

public class RelativeStrengthIndex extends AbstractIndicator<Double, BaseIndicatorConfig>{
	
	public static final RelativeStrengthIndex INSTANCE = new RelativeStrengthIndex();
	
	private RelativeStrengthIndex() {
		super("RSI");
	}

	@Override
	public IndicatorPosition getPosition(BaseIndicatorConfig config) {
		return IndicatorPosition.INDEPENDENT;
	}

	@Override
	public IndicatorDisplayType getDisplayType(BaseIndicatorConfig config) {
		return IndicatorDisplayType.LINE;
	}

	@Override
	public Double getValue(BaseIndicatorConfig configuration, Double[] in, int index) throws UnsupportedOperationException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Double[] getValues(BaseIndicatorConfig configuration, Double[] in) throws UnsupportedOperationException {
		double avgGain = 0;
		double avgLoss = 0;
		Double[] result = new Double[in.length];
		int span = configuration.getSpan();
		for(int index = in.length - 2; index >= in.length - 1 - span; index--){
			double change = in[index] - in[index+1];
			if(0 < change){
				avgGain += change;
			}else{
				avgLoss += -1*change;
			}
		}
		avgGain /= span;
		avgLoss /= span;
		for(int index = in.length - 2 - span; index >=0; index--){
			double change = in[index] - in[index+1];
			if(0 < change){
				avgGain = (avgGain*(span - 1)+change)/span;
			}else{
				avgLoss = (avgLoss*(span - 1)+(-1*change))/span;
			}
			double rs = avgGain/avgLoss;
			result[index] = 100 - (100/(1+rs));
		}
		return result;
	}

	
	@Override
	public BaseIndicatorConfig createDefaultConfiguration() {
		return new BaseIndicatorConfig();
	}
}
